package com.starlink.common.service;

import com.google.common.collect.Lists;
import com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.CommonCreateOrderResponse;
import com.starlink.client.jd.JdClient;
import com.starlink.client.jd.properties.JdProperties;
import com.starlink.client.jd.request.CreateOrderRequest;
import com.starlink.client.jd.request.PreCheckRequest;
import com.starlink.client.jd.request.SubscribeTraceRequest;
import com.starlink.client.jd.request.base.CommonCargoInfo;
import com.starlink.client.jd.request.base.CommonProductInfo;
import com.starlink.client.jd.request.base.Contact;
import com.starlink.model.domain.AddressDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class JdService {

    private static final String PRODUCT_CODE = "ed-m-0001";

    @Autowired
    private JdClient jdClient;
    @Autowired
    private JdProperties jdProperties;

    /**
     * 创建京东订单
     * @param num 订单物品件数
     * @param description 拖寄物描述
     * @param sender 发件人
     * @param receiver 收件人
     * @param weight 重量；单位：kg
     * @param volume 体积：单位：cm³
     * @param logisticsOrderNum 商家物流订单编号
     * @return
     */
    public CommonCreateOrderResponse creatJDOrder(Integer num,
                                                   String description,
                                                   Contact sender,
                                                   Contact receiver,
                                                   AtomicReference<BigDecimal> weight,
                                                   AtomicReference<BigDecimal> volume,
                                                   String logisticsOrderNum) {
        // 前置校验
        jdPreCheck(sender, receiver);

        // 物流下单
        CreateOrderRequest createOrderRequest = new CreateOrderRequest();
        createOrderRequest.setOrderId(logisticsOrderNum);
        createOrderRequest.setSenderContact(sender);
        createOrderRequest.setReceiverContact(receiver);
        createOrderRequest.setOrderOrigin(1);
        createOrderRequest.setCustomerCode(jdProperties.getCustomerCode());
        CommonProductInfo productInfo = new CommonProductInfo();
        productInfo.setProductCode(PRODUCT_CODE);
        createOrderRequest.setProductsReq(productInfo);
        createOrderRequest.setSettleType(3);
        List<CommonCargoInfo> cargoInfos = Lists.newArrayList();
        CommonCargoInfo cargoInfo = new CommonCargoInfo();
        cargoInfo.setName(description);
        cargoInfo.setQuantity(num);
        cargoInfo.setWeight(weight.get());
        cargoInfo.setVolume(volume.get());
        cargoInfos.add(cargoInfo);
        createOrderRequest.setCargoes(cargoInfos);
        createOrderRequest.setRemark("");
        CommonCreateOrderResponse response = jdClient.create(createOrderRequest);

        //订阅物流轨迹 todo 订阅物流轨迹失败（后续处理，从新订阅或者取消已经下单的物流单）
        SubscribeTraceRequest traceRequest = new SubscribeTraceRequest();
        traceRequest.setMobile(receiver.getMobile().substring(receiver.getMobile().length() - 4));
        traceRequest.setWaybillCode(response.getWaybillCode());
        traceRequest.setOrderCode(response.getOrderCode());
        traceRequest.setOrderOrigin(1);
        traceRequest.setCustomerCode(jdProperties.getCustomerCode());
        jdClient.subscribeTrace(traceRequest);
        return response;
    }

    private void jdPreCheck(Contact sender, Contact receiver) {
        PreCheckRequest preCheckRequest = new PreCheckRequest();
        preCheckRequest.setSenderContact(sender);
        preCheckRequest.setReceiverContact(receiver);
        preCheckRequest.setOrderOrigin(1);
        preCheckRequest.setCustomerCode(jdProperties.getCustomerCode());
        CommonProductInfo productInfo = new CommonProductInfo();
        productInfo.setProductCode(PRODUCT_CODE);
        preCheckRequest.setProductsReq(productInfo);
        jdClient.preCheck(preCheckRequest);
    }
}
